$(function() {
	$("#top_menu").load("../../uiframe/Top.html", function(){
		$("#xitongshezhi").addClass("active");
	}); 
	// 加载用户列表
	var urlStr = serverPath + "/server/login.do";
	var jsonStr = '{method:"userList";curpage:1}';

	UseAjax(urlStr, jsonStr, document.cookie, task, '1');
	
});

//缓存用户列表
var userArray;
var curpage=1;
//处理用户列表
function task(nodeArray) {
	
	$('#person').hide();
	if (nodeArray && nodeArray.resultList.length > 0) {
		userArray = nodeArray;
		//排序
		userlist_=nodeArray.resultList;
		var text = '';
		for ( var i = 0; i < nodeArray.resultList.length; i++) {
			var wi = nodeArray.resultList[i];
			if (i == 0) {
				text = text + '<table class="table">';
				text = text + '<tr class="odd first-row"><td><a href="javascript:sort(\'sortname\')">姓名</a></td><td><a href="javascript:sort(\'sortlogin\')">登录名</a></td><td>角色</td><td colspan="1">操作</td></tr>';
			}
			text = text + '<tr class="odd"><td>' + wi.username + '</td><td>' + wi.loginid
					+ '</td><td>' + wi.roleName
					+ '</td><td><a href="javascript:toedit(\''+wi.loginid+'\')">编辑</a><a href="javascript:deluser(\''+wi.id+'\')">&nbsp;&nbsp;删除</a></td></tr>';
		}
		//翻页
		text = text +'<tr><td colspan=5>共'+nodeArray.count+'条'+nodeArray.pages+'页，当前第'+curpage+'页';
		if(curpage==1 && nodeArray.pages>1){
			text = text +'<a href="javascript:gopage(\''+(parseInt(curpage)+1)+'\')">下一页</a>';
		}else if(curpage==nodeArray.pages  && nodeArray.pages>1){
			text = text +'<a href="javascript:gopage(\''+(parseInt(curpage)-1)+'\')">上一页</a>';
		}else if(nodeArray.pages>1){
			text = text +'<a href="javascript:gopage(\''+(parseInt(curpage)-1)+'\')">上一页</a>';
			text = text +'<a href="javascript:gopage(\''+(parseInt(curpage)+1)+'\')">下一页</a>';
		}
		text = text + '<a href="javascript:getRole()">&nbsp;&nbsp;添加</a></td></tr>';
		text = text + '</table>';
		$('#task').html(text);
	} else {
		$('#task')
				.html(
						'<div class="tasklist1" ><div class="tasklistbt" ><div class="taskfont" style="padding-left:280px;">没有记录</div></div></div>');
	}

}
//按姓名排序
function sortname(a,b)
{
	return a.username.localeCompare(b.username);
}
//按登录名排序
function sortlogin(a,b)
{
	return b.loginid-a.loginid;
}
//用户排序
function sort(sortStatus) {
	task(userArray,sortStatus);
}
//获取角色
function getRole(){
	var urlStr = serverPath + "/server/login.do";
	var jsonStr = '{method:"getRole"}';
	UseAjax(urlStr, jsonStr, document.cookie, newuser, '1');
}
//添加用户
function newuser(r) {
	   $('#person').show();
		var text = '';
		text = text + '<form id="person2" method="post">';
		text = text + '<table class="table">';
				text = text + '<tr><td>姓名</td><td><input  id="user_username"  name="user_username"  class="easyui-validatebox" required="true" validType="length[1,11]" missingMessage="不能为空" invalidMessage="登录名为11位以下" type="text" value=""></td>';
				text = text + '</td><td>登录名</td><td><input  id="user_loginid"  name="user_loginid"  value="" type="text"></td></tr>';
				text = text + '<tr><td>密码</td><td><input  id="user_loginpassword" name="user_loginpassword" class="easyui-validatebox"  type="password"  value=""></td>';
				text = text + '<td>角色</td><td>';
//				text = text + '<input  id="user_roleid"  name="user_roleid"  class="easyui-validatebox" required="true"  missingMessage="不能为空" type="text"  value="">';
				text = text + '<select id="user_roleid" name="user_roleid"><option value="0">请选择</option>';
				for(var i=0;i<r.roleList.length;i++){
					text = text + '<option value="'+r.roleList[i].id+'">'+r.roleList[i].role_name+'</option>';
				}
				text = text +'</select>';
				text = text + '</td></tr>';
				text = text + '</table>';
				text = text + '<div class="usermng-sbm"><input type="button" class="usermng-btn" value="提交" onclick="javascript:doadd();"/><td><input type="button" class="cancel usermng-btn" value="取消" onclick="javascript:location.reload();"/></div>';
			text = text + '</form>';
			
		$('#person').html(text);
}

function doadd() {
	var userName = $("#user_username").val();
	var loginid = $("#user_loginid").val();
	var loginpassword = $("#user_loginpassword").val();
	var roleid = $("#user_roleid").val();
	if(trim(userName).length==0){
		alert("名称不能为空，请填写!");
		return;
	}else if(trim(loginid).length==0){
		alert("登录名称不能为空，请填写！");
		return;
	}else if(trim(loginpassword).length==0){
		alert("登录密码不能为空，请填写！");
		return;
	}else if(roleid==0){
		alert("请关联角色！");
		return;
	}else if(userName.length>0&&loginid.length>0&&loginpassword.length>0&&roleid.length>0){
		var urlStr = serverPath + "/server/login.do";
		var jsonStr = '{method:"adduser",user.username:"' + $("#user_username").val()
				+ '",user.loginid:"' + $("#user_loginid").val() 
				+ '",user.loginpassword:"' + $("#user_loginpassword").val() 
				+ '",user.roleid:"' + $("#user_roleid").val() + '"}';
		//添加成功返回列表
		UseAjax(urlStr, jsonStr, document.cookie, task, '1');
	}
}
function toedit(loginid){
	var urlStr = serverPath + "/server/login.do";
	var jsonStr = '{method:"getthisUser",loginid:"'+loginid+'"}';
	UseAjax(urlStr, jsonStr, document.cookie, edit, '1');
}
//用户编辑
function edit(data) {
	if (data && data.userList.length > 0) {
		 $('#person').show();
		var text = '';
	        text = text + '<form id="person2" method="post">';
			text = text + '<table class="table">';
			text = text + '<tr><td>姓名</td><td><input  id="user_username"  name="user_username"  class="easyui-validatebox" required="true" validType="length[1,11]" missingMessage="不能为空" invalidMessage="登录名为11位以下" type="text" value="'+data.userList[0].username+'"></td>';
			text = text + '</td><td></td><td><input  id="user_loginid"  name="user_loginid"  value="'+data.userList[0].loginid+'" type="hidden"></td></tr>';
			text = text + '<tr><td>密码</td><td><input  id="user_loginpassword" name="user_loginpassword" class="easyui-validatebox"  type="password"  value="'+data.userList[0].loginpassword+'"></td>';
			text = text + '<td>角色</td><td>';
			text = text + '<select id="user_roleid" name="user_roleid"><option value="0">请选择</option>';
				for(var i=0;i<data.roleList.length;i++){
					text = text + '<option value="'+data.roleList[i].id+'">'+data.roleList[i].role_name+'</option>';
				}
				text = text +'</select>';
				text = text + '</td></tr>';
				text = text + '</table>';
				text = text + '<div class="usermng-sbm"><input type="button" class="usermng-btn" value="提交" onclick="javascript:doedit();" /><td><input type="button" class="cancel usermng-btn" value="取消" onclick="javascript:location.reload();"/></div>';
			text = text + '</form>';
		$('#person').html(text);
		$("#user_roleid").attr("value", data.userList[0].roleid);
	} else {
		$('#person').html('请选择一条记录');
	}
}
//提交编辑结果
function submedit(){
	$('#person2').form( {
		onSubmit : function() {
			// 进行表单验证
		// 如果返回false阻止提交
		return $(this).form('validate');
	},
	success : function(data) {
		doedit();
	}
	});
	$('#person2').submit();
}

function doedit() {
	var userName = $("#user_username").val();
	var loginpassword = $("#user_loginpassword").val();
	var roleid = $("#user_roleid").val();
	if(trim(userName).length==0){
		alert("名称不能为空!");
		return;
	}else if(trim(loginpassword).length==0){
		alert("登录密码不能为空！");
		return;
	}else if(roleid==0){
		alert("关联角色不能为空！");
		return;
	}else if(trim(userName).length>0&&(loginpassword).length>0&&roleid.length>0){
	var urlStr = serverPath + "/server/login.do";
	var jsonStr = '{method:"edituser",user.username:"' + $("#user_username").val()
			+ '",user.loginid:"' + $("#user_loginid").val() 
			+ '",user.loginpassword:"' + $("#user_loginpassword").val() 
			+ '",user.roleid:"' + $("#user_roleid").val() + '"}';
	//编辑成功返回列表
	UseAjax(urlStr, jsonStr, document.cookie, task, '1');
	}
}

//删除用户
function deluser(id) {
	if(confirm("是否删除,删除后该用户信息将不存在！")){
		var urlStr = serverPath + "/server/login.do";
		var jsonStr = '{method:"deluser",user.id:"' + id  + '"}';
		//删除成功返回列表
		UseAjax(urlStr, jsonStr, document.cookie, task, '1');
	}
}


function exportuser() {
	var urlStr = serverPath + "/server/login.do";
	var jsonStr = '{method:"exportuser"}';
	//编辑成功返回列表
	UseAjax(urlStr, jsonStr, document.cookie, exportfile, '1');
}
function  exportfile(nodeArray) {
	if (nodeArray && nodeArray.result.length > 0) {
		alert(nodeArray.result);
		window.open(contextPath+nodeArray.result);
	}
}
//分页方法
function gopage(page) {
	curpage = page;
	var urlStr = serverPath + "/server/login.do";
	var jsonStr = '{method:"userList";curpage:'+curpage+'}';
	UseAjax(urlStr, jsonStr, document.cookie, task, '1');
}
//去掉字符串中左右两端的空格
function trim(str){
    return str.replace(/(^\s*)|(\s*$)/g, "");  
}